define(function (require, exports, module) {
    require("apps/process/page.css");
    require("apps/easyui");
    require("apps/file");

   var indicator = $('<div class="indicator">>></div>').appendTo('body');
   function initSort(){
       $('tr.drag-item').draggable({
           handle:"td.drag-handle",
           revert:true,
           deltaX:0,
           deltaY:0
       }).droppable({
           onDragOver:function(e,source){
               indicator.css({
                   display:'block',
                   left:$(this).offset().left-10,
                   top:$(this).offset().top+$(this).outerHeight()-5
               });
           },
           onDragLeave:function(e,source){
               indicator.hide();
           },
           onDrop:function(e,source){
               $(source).insertAfter(this);
               indicator.hide();
           }
       });
   }
    exports.initSort = initSort;
    exports.init = function(ctx, uuid, config){
        var page = new $.PageDomFactory(uuid);
        $(".easyui-validatebox").validatebox();
        initSort();
        page.getJqDom('uploadCover').cropImageUpload({
            imgRange: [
                {
                    id: "normal",
                    width: 128,
                    height: 128
                },
                {
                    id: "tiny",
                    width: 64,
                    height: 64
                }
            ],
            buttonWidth: 100,
            selectText: "选择模板封面",
            onCropComplete: function (event, ins) {
                var file = ins.file;
                page.getJqDom('uploadCover').val(file);
                page.getJqDom('normal').attr("src", WE.contextPath + "/print" + file);
                page.getJqDom('tiny').attr("src", WE.contextPath + "/print" + file);
            }
        });
        var types = ["操作人员", "操作时间", "执行结果"];
        page.getJqDom("save-btn").linkbutton({
            onClick:function(){
                var validate = page.getJqDom("base-form").form("validate");
                if(!validate){
                    return false;
                }
                validate = $.fn.form.methods.validate(page.getJqDom("table-attr"));
                if(!validate){
                    return false;
                }
                var baseData = page.getJqDom("base-form").serializeJson();
                baseData.tProcessAttributes = [];
                page.getJqDom("table-attr").find("tr").each(function(){
                    var json = $(this).serializeJson();
                    if(json.fieldType == 'FIELD_TYPE_TABLE'){
                        json.fieldTypeParam = json.fieldTypeParam1;
                    }else if(json.fieldType == 'FIELD_TYPE_DIRECTORY'){
                        json.fieldTypeParam = json.fieldTypeParam2;
                    }
                    delete json.fieldTypeParam1;
                    delete json.fieldTypeParam2;
                    baseData.tProcessAttributes.push(json);
                });
                var keySet = {}, errorMsgs = ["<br />"];
                for(var i= 0, l = baseData.tProcessAttributes.length; i < l; i++){
                    var setV = keySet[baseData.tProcessAttributes[i].fieldName];
                    if(setV){
                        errorMsgs.push("“第" + (i + 1) + "个属性和第" + setV + "个属性重复”<br />");
                    }else{
                        keySet[baseData.tProcessAttributes[i].fieldName] = i + 1;
                    }
                    var ii = types.indexOf(baseData.tProcessAttributes[i].fieldName);
                    if(ii >= 0){
                        errorMsgs.push("“第" + (i + 1) + "个属性使用了关键字" + types[ii] + "”<br />");
                    }
                }
                if(errorMsgs.length > 1){
                    $.messager.alert('信息',
                            "数据保存过程出检测到如下错误" + errorMsgs.join("") + ",请修改后重新保存", 'info');
                    return;
                }

                baseData = $.utils.buildJson(baseData);
                $("body").wait("start", "模板保存中，请稍后！");
                $.post(config.postUrl, baseData, function(data){
                    $("body").wait("end");
                    if ($.isPlainObject(data) && data.error) {
                        $.messager.alert('信息',
                            data.msg, 'info');
                    } else {
                        $.messager.alert('信息',
                            '保存成功', 'info');
                        window.location.href = WE.contextPath + "/app/api/process/index"
                    }
                }, "JSON");
            }
        });
        page.getJqDom("cancel-btn").linkbutton({
            onClick:function(){
                window.location.href = WE.contextPath + "/app/api/process/index";
            }
        });
    };

    return exports;
});